﻿using System;
using System.Collections.Generic;
using SrBBSPro.Entities;
using SrBBSPro.BaseClasses;

namespace SrBBSPro.Services
{
    public class CountService : BaseService<HolidayCount, long> 
    {

        public List<HolidayCount> GetCountList(string name, int page, out int count)
        {
            var li = DbBase.Query<HolidayCount>().OrderByDescending(h => h.StartDate).ToList(); ;
            if (!string.IsNullOrEmpty(name))
            {
                li = li.FindAll(h => h.Title.Contains(name));
            }
            return GetLimitList(li, out count, page);
        }

        /// <summary>
        /// 获取已发布的休假信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HolidayCount GetHolidayCountById(long id)
            => DbBase.SingleOrDefaultById<HolidayCount>(id);

        /// <summary>
        /// 新增休假发布
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Response DelCount(int hid)
        {
            var res = new Response();
            try
            {
                using (var transaction = DbBase.GetTransaction())
                {
                    var num = DbBase.Delete<HolidayCount>(hid);
                    if (num > 0)
                    {
                        DbBase.DeleteMany<HolidayNote>().Where(h => h.Id == hid);
                        res.IsSuccess = true;
                        res.Message = "删除成功";
                    }
                    else
                    {
                        res.Message = "删除失败";
                    }
                    transaction.Complete();                   
                }
            }
            catch
            {
                res.Message = "删除失败";
            }
            return res;
        }      
    }
}
